var module = angular.module('app.controllers', []);
module.controller('MainController',function ($scope,API,$state,$ionicSlideBoxDelegate) {
    API.getCateList({
        sign_shop : 'xykr',
    },function(err,result){
        if(result){
            $scope.CateList = result;
        }
    });

    $scope.query = {};
    $scope.query.query = '';
    API.getStatusGoodslist({
        sign_shop : 'xykr',
        status : 'sytj',
        pagesize : 100
    },function(err,result){
        if(result){
            $scope.StatusGoodslist = result;
        }
    });
    //console.log($localStorage);
    $scope.$emit('hideNav',false);

    $scope.myActiveSlide = 1;
    $scope.plusnum = function(){
        this.item.num = (this.item.num)?this.item.num+1:1;
    }
    $scope.minusnum = function(){
        this.item.num = (this.item.num > 0)?this.item.num-1:0;
    }

    $scope.search = function(){
        $state.go('index');
        // $state.go('search',{query:$scope.query.query});
    }
    API.getSignAds({
        sign : 'index_banner'
    },function(err,result){
        $scope.SignAds = result.data.SignAds;
        $ionicSlideBoxDelegate.$getByHandle('SignAds').update();
        $ionicSlideBoxDelegate.$getByHandle('SignAds').loop(true);
    });
    API.getSignAds({
        sign : 'index_ads'
    },function(err,result){
        $scope.index_ads = result.data.SignAds;
    });
});



module.controller('PayController', function ($scope, API,$base64,$stateParams,$state,$window ,$ionicPopup,$rootScope,$interval,$ionicLoading,loadConfig) {
    if(!$stateParams.info){
        return $state.go('index');
    }
    $ionicLoading.show(loadConfig);
    var timeout = $interval(function(){
        if($rootScope.share && !$scope.success){
            $scope.success = true;
            $ionicLoading.hide();
            wx.chooseWXPay({
                appId : wepay.appId,
                timestamp: wepay.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                nonceStr: wepay.nonceStr, // 支付签名随机串，不长于 32 位
                package: wepay.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
                signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                paySign: wepay.paySign, // 支付签名
                success: function (res) {
                    $scope.alert('支付成功');
                    if(order_id){
                        $state.go('order.info',{order_id:order_id});
                    }else{
                        if(vip){
                            $state.go('start');
                        }else if(vip_cach){
                            $state.go('start');
                        }else if(start){
                            $state.go('start');
                        }else{
                            $state.go('order.index');
                        }
                    }
                    // 支付成功后的回调函数
                },
                cancel: function (){
                    if(order_id){
                        $state.go('order.info',{order_id:order_id});
                    }else{
                        if(vip){
                            $state.go('start');
                        }else if(vip_cach){
                            $state.go('start');
                        }else if(start){
                            $state.go('start');
                        }else{
                            $state.go('order.index');
                        }
                    }
                },
                error: function (e) {
                    if(order_id){
                        $state.go('order.info',{order_id:order_id});
                    }else{
                        if(vip){
                            $state.go('start');
                        }else if(vip_cach){
                            $state.go('start');
                        }else if(start){
                            $state.go('start');
                        }else{
                            $state.go('order.index');
                        }
                    }
                }

            });
        }
    },100);
    var wepay = $base64.decode($stateParams.info);
    wepay = angular.fromJson(wepay);
    var order_id = wepay.order_id;
    var vip = wepay.vip;
    var vip_cach = wepay.vip_cach;
    var start = wepay.start;
    console.log(start);
    $scope.alert = function(msg){
        var confirm = $ionicPopup.confirm({
            title: '消息提示',
            template : msg,
            buttons: [
                {
                    text: '<b>确定</b>',
                    type: 'button-positive'
                }
            ]
        });
    };

});

module.controller('IndexController', ['$scope', 'ConfigModule', '$http', '$location',
    function ($scope, ConfigModule, $http, $location) {
        $scope.$on('hideNav',function(even,data){
            $scope.hideNav = data;
        });
        $scope.order = function(){
            $location.path('/order/index');
        }
        $scope.member = function(){
            $location.path('/member/index');
        }
        $scope.index = function(){
            $location.path('/start');
        }
        $scope.vip = function(){
            $location.path('/vip/index');
        }
    }]
);

module.controller('StartController', function ($scope,API,$state,$ionicSlideBoxDelegate,$ionicPopup,$window,$base64 ) {

    $scope.query = {};
    $scope.query.query = '';
    //console.log($localStorage);
    $scope.$emit('hideNav',false);

    $scope.myActiveSlide = 1;

    $scope.search = function(){
        $state.go('index');
        // $state.go('search',{query:$scope.query.query});
    }
    API.getSignAds({
        sign : 'dh_banner'
    },function(err,result){
        $scope.SignAds = result.data.SignAds;
        $ionicSlideBoxDelegate.$getByHandle('SignAds').update();
        $ionicSlideBoxDelegate.$getByHandle('SignAds').loop(true);
    });
    API.getSignAds({
        sign : 'index_ads'
    },function(err,result){
        $scope.index_ads = result.data.SignAds;
    });
    API.getIndexStoreList(function(err,result){
        $scope.index_list = result.data.index_list;
    });

    $scope.pay_type = {};
    $scope.pay_type.pay_type = 1;

    // $scope.alert = function(msg){
    //     var confirm = $ionicPopup.confirm({
    //         title: '消息提示',
    //         template : msg,
    //         buttons: [
    //             {
    //                 text: '<b>确定</b>',
    //                 type: 'button-positive'
    //             }
    //         ]
    //     });
    // };
    // $scope.pay = function(vip_type){
    //
    //     var confirm = $ionicPopup.confirm({
    //         title: '选择支付方式',
    //         templateUrl: 'tpl/public/check_pay2.html',
    //         okText : '确定',
    //         cancelText : '取消',
    //         scope : $scope
    //     });
    //     confirm.then(function(res) {
    //         if(res) {
    //             if($scope.pay_type.pay_type){
    //                 API.vipRecharge({
    //                     vip_type : vip_type,
    //                     pay_type : $scope.pay_type.pay_type
    //                 },function(err,result){
    //                     if(result.status == 200){
    //                         if($scope.pay_type.pay_type == 1){
    //                             var wepay = result.data;
    //                             wepay.start = true;
    //                             var info = angular.toJson(wepay);
    //                             $window.location.href='http://www.hjfzqy.cn/#/pay?info='+$base64.encode(info);
    //                         }else{
    //                             $scope.alert('支付成功');
    //                             $state.reload();
    //                         }
    //                     }else{
    //                         $scope.alert(result.message);
    //                     }
    //                 });
    //             }
    //         }
    //     });
    // };


});

module.controller('SearchController', function ($scope, API,$stateParams,$state) {
    if(!$stateParams.query){
        return $state.go('index');
    }
    $scope.query = {};
    $scope.query.query = $stateParams.query;
    $scope.$emit('hideNav',false);
    API.search({
        query : $stateParams.query
    },function(err,result){
        $scope.goods_list = result.data.goods_list;
    });
    $scope.search = function(){
        $state.go('search',{query:$scope.query.query});
    }

});

module.controller('BdsyController', function ($scope, API,$stateParams,$state) {
    console.log(123);

    $scope.$emit('hideNav',true);
});
